abstract class AddSpacesToPrice {
  String call({
    required String price,
    required String delimiter,
  });
}

class AddSpacesToPriceImpl implements AddSpacesToPrice {
  const AddSpacesToPriceImpl();

  @override
  String call({
    required String price,
    required String delimiter,
  }) {
    final priceParts = price.split(delimiter);
    final priceFirstPart = priceParts.first;

    var updatedPriceFirstPart = '';

    for (var i = 0; i < priceFirstPart.length; i++) {
      updatedPriceFirstPart = priceFirstPart[priceFirstPart.length - 1 - i] + updatedPriceFirstPart;
      if (i % 3 == 2 && i != priceFirstPart.length - 1) {
        updatedPriceFirstPart = ' $updatedPriceFirstPart';
      }
    }

    if (priceParts.length == 1) {
      return updatedPriceFirstPart;
    }

    return '$updatedPriceFirstPart.${priceParts.last}';
  }
}
